%%%----------------------------------------------------------------------
%%% File    : master.erl
%%% Author  : Sunny <sunjiangangok@gmail.com>
%%% Purpose : 
%%% Created : 8 Apr 2011 by Sunny <sunjiangangok@gmail.com>
%%% Id      :
%%%----------------------------------------------------------------------

-module(master).
-author('sunjiangangok@gmail.com').

-behaviour(gen_server).

-include("record.hrl").

%% External exports
-export([start/0,
	stop/0,
	user_request/4]).

%% gen_server callbacks
-export([init/1,
	handle_call/3,
	handle_cast/2,
	code_change/3,
	handle_info/2,
	terminate/2]).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
	gen_server:call(?MODULE, {user_request, stop}).
user_request(FsmName, Command, Nc, VM_num) ->
	MASTER_VM_START = #master_vm_start{fsmname=FsmName, command=Command, nc=Nc, vm_num=VM_num},
	gen_server:call(?MODULE, {user_request, MASTER_VM_START}).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%%----------------------------------------------------------------------
init([]) ->
	master_mnesia:start(),
    {ok, ok}.

%%----------------------------------------------------------------------
%% Func: handle_call/3
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%%----------------------------------------------------------------------
handle_call({user_request, stop}, From, State) ->
	Reply = ok,
	{stop, normal, Reply, State};
handle_call({user_request, MASTER_VM_START}, From, State) ->
%	io:format("~p    ~p    ~p     ~p~n", [MASTER_VM_START, From, State, self()]),
%% Create a process to process the user's request, and main process respond request.
%	Pid = spawn(fun() ->  master_pro_req:pro_start(MASTER_VM_START) end),
	master_pro_req:pro_start(MASTER_VM_START),
	Reply = ok,
    {reply, Reply, State}.

%%----------------------------------------------------------------------
%% Func: handle_cast/2
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%%----------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.


code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%----------------------------------------------------------------------
%% Func: handle_info/2
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%%----------------------------------------------------------------------
handle_info(_Info, State) ->
    {noreply, State}.

%%----------------------------------------------------------------------
%% Func: terminate/2
%% Purpose: Shutdown the server
%% Returns: any (ignored by gen_server)
%%----------------------------------------------------------------------
terminate(_Reason, _State) ->
	master_mnesia:stop(),
    ok.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------

%%Internal functions
